<title>规格值管理</title>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="goods-spec_value-index-search_from">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">名称</label>
                    <div class="layui-input-inline">
                        <input type="text" name="value" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-list" lay-submit lay-filter="goods-spec_value-index-search_button">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                </div>
            </div>
        </div>

        <script type="text/html" id="goods-spec_value-index-toolbar_tmp">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-sm" lay-event="goods-spec_value-index-add">添加</button>
                <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="goods-spec_value-index-del">删除</button>
            </div>
        </script>
        <div class="layui-card-body">
            <table id="goods-spec_value-index" lay-filter="goods-spec_value-index"></table>
            <script type="text/html" id="goods-spec_value-index-action_button">
                <a class="layui-btn layui-btn-xs" lay-event="goods-spec_value-index-edit">编辑</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="goods-spec_value-index-del">删除</a>
            </script>
        </div>
    </div>
</div>
<script type="text/html" template lay-done="layui.data.sendParams(d.params)"></script>
<script>
    layui.data.sendParams = function (params) {
        layui.use(['common', 'table'], function () {
            var $ = layui.jquery,
                common = layui.common,
                view = layui.view,
                table = layui.table,
                form = layui.form,
                lay_id_prefix = 'goods-spec_value-index',//当前页id前缀
                edit_id_prefix = 'goods-spec_value-add',//添加编辑页id前缀
                edit_view_url = 'goods/spec_value/add',//添加编辑页模板地址
                api_action_url = layui.setter.apiHost + '/goods/spec_value';

            //表单搜索
            form.on('submit(' + lay_id_prefix + '-search_button)', function (data) {
                var field = data.field;
                field.page = 1;//默认第一页
                table.reload(lay_id_prefix, {
                    where: field
                });
            });

            //表格操作
            table.render({
                elem: '#' + lay_id_prefix,
                url: api_action_url,
                where: {spec_id: params.spec_id},
                toolbar: '#' + lay_id_prefix + '-toolbar_tmp',
                cols: [[
                    {type: 'checkbox', fixed: 'left'},
                    {field: 'id', title: 'ID', width: 80},
                    {field: 'value', title: '名称', minWidth: 200},
                    {field: 'position', title: '排序', width: 80, sort: true, edit: 'text'},
                    {title: '操作', width: 150, align: 'center', fixed: 'right', toolbar: '#' + lay_id_prefix + '-action_button'}
                ]],
                page: true
            });

            //头部工具栏操作
            table.on('toolbar(' + lay_id_prefix + ')', function (obj) {
                var type = obj.event;
                switch (type) {
                    case lay_id_prefix + '-add':
                        common.open_window('添加', api_action_url, edit_view_url, edit_id_prefix, {table_id: lay_id_prefix}, {spec_id: params.spec_id});
                        break;
                    case lay_id_prefix + '-del':
                        layer.confirm('确定删除吗', function (index) {
                            common.ajax(api_action_url + '/delete', lay_id_prefix);
                        });
                        break;
                }
            });

            //监听工具条操作按钮
            table.on('tool(' + lay_id_prefix + ')', function (obj) {
                var data = obj.data;
                switch (obj.event) {
                    case lay_id_prefix + '-edit':
                        var group_data = {group_id: params.group_id, width: params.width, height: params.height};
                        common.open_window('编辑', api_action_url, edit_view_url, edit_id_prefix, {table_id: lay_id_prefix, id: data.id}, {spec_id: params.spec_id});
                        break;
                    case lay_id_prefix + '-del':
                        layer.confirm('确定删除吗', function (index) {
                            if (common.ajax(api_action_url + '/delete', lay_id_prefix, {id: data.id}, false)) {
                                obj.del();
                                layer.close(index);
                            }
                        });
                        break;
                }
            });

            //监听单元格编辑
            table.on('edit(' + lay_id_prefix + ')', function (obj) {
                var send_data = {id: obj.data.id, field: obj.field, field_value: obj.value};
                common.ajax(api_action_url + '/field_update', lay_id_prefix, send_data, false);
            });

        });
    }
</script>